import bpy

import engine.initLIB.load_dll as engineLib
import ctypes

import 全局控制.Node.节点和插座基类.bl_节点基类 as 基类



engineLib.flexCAPI.f_创建取物体材质节点.restype = ctypes.c_bool





class C_Node_取物体材质(基类.C_Node_基类):
	bl_dname = 'bl_node_取物体材质'
	bl_label = '取物体材质'
	
	def init(self, context):
		super(C_Node_取物体材质, self).f_init(context)
		self.inputs.new('C_Socket_物体_Array1D', name='物体')
		self.inputs.new('C_Socket_I32_Array1D', name='ID')
		self.outputs.new('C_Socket_材质_Array1D', name='材质')
		#self.inputs['材质槽ID'].f_setData(-1)
		self.f_toCPP()
		
	def f_toCPP(self):
		是否更新 = engineLib.flexCAPI.f_创建取物体材质节点(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), self.f_getPyObject());
		if 是否更新 :
			self.inputs['ID'].f_update()

	def f_update(self):
		objs = self.inputs['物体'].f_getData()
		IDs = self.inputs['ID'].f_getData()
		#材质名称 = self.inputs['材质名称'].f_getData()
		if len(IDs) <= 0 : IDs = [0]
		
		materials = []
		for obj in objs:
			matID = IDs[0]
			if matID < 0 :
				for m in obj.material_slots:
					materials.append([m, m.name])
			else:
				mat = obj.material_slots[matID]
				materials.append([mat, mat.name])
			
		self.outputs['材质'].f_setData(materials)


class C_NODE_AddMenu_取物体材质(bpy.types.Operator):
	bl_idname = 'node.global_node_get_obj_material'
	bl_label = '取物体材质'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_取物体材质', use_transform=True)
		return {'FINISHED'}













classes = (
	C_Node_取物体材质,
	C_NODE_AddMenu_取物体材质,

)


def register():
	for c in classes:
		bpy.utils.register_class(c)
		
def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)

	


